<?php
    namespace App\common;

    class cJson 
	{
        private $m_s                ; # original json string
        private $m_o                ; # stdClass
        private $m_ar_decode_status ; # decode status
	 	
        public function getOriginJsonString($callback_original_json_string=null)
		{
            if(null==$callback_original_json_string)
			{
                return $this->m_s;             
            }
			else
			{
                $callback_original_json_string($this->m_s);
                return $this;
            }
        }
        public function __get($name)
		{
            return $this->get($name);
        }
        public function get($key,$callback_get=null)
		{
            $r = @$this->m_o->$key;if(null==$callback_get)
			{
                return $r;
            }
			else
			{
                $callback_get($r);
                return $this;
            }
        }
        public function getDecodeStatus($callback_decode_status=null)
		{
            if(null==$callback_decode_status)
			{
                return cHelper::createHelper()->gfApiEchoReturn($this->m_ar_decode_status['data'],$this->m_ar_decode_status['code'],$this->m_ar_decode_status['message']);
            }
			else
			{
                $callback_decode_status($this->m_ar_decode_status['data'],$this->m_ar_decode_status['code'],$this->m_ar_decode_status['message']);
                return $this;
            }                
        }
        public function setDecodeStatusWithJsonError($code,$message)
		{
			return $this->setDecodeStatus([['json.error : ' => $this->getOriginJsonString()]],$code,$message);
		}
        public function setDecodeStatus($data,$code,$message)
		{
            $this->m_ar_decode_status['data'    ] = $data   ;
            $this->m_ar_decode_status['code'    ] = $code   ;
            $this->m_ar_decode_status['message' ] = $message;
            return $this;
        }

		/* append some extension property to object
		 *
		 */
		public function appendProperty($property_key_and_value=[])
		{
			foreach($property_key_and_value as $k => $v)
			{
				$this->m_o->$k	= $v;
			}
			return $this;
		}

		/* decode json to standard class object
		 *
		 */
        public function decode($s,$callback_ok/* ok */,$callback_error/* error */,$debug)
		{
			# save current original json string
			#
            $this->m_s  = $s;

			# try to decode json to object
			#
            $o          = json_decode($s);if(null==$o)
			{
                # error
                #
                if(null==$callback_error){}else{$callback_error($this);}
            }
			else
			{
				# save current object
				#
                $this->m_o = $o;
               
                # ok 
                #
                if(null==$callback_ok){}else{$callback_ok($this);}
            }

			#
			#
            return $this;
        }
        public function getAllJsonDataToObject($callback_get_all=null,$debug=false)
		{
			return $this->m_o;
		}

		/* decode json to array
		 *
		 */
        public function getAllJsonData($callback_get_all=null,$debug=false)
		{
			$ar	= json_decode($this->m_s,true);if($callback_get_all==null)
			{
				return $ar;
			}
			else
			{
				$callback_get_all($ar);
				return $this;
			}
        }
		public function toArray()
		{
			return $this->getAllJsonData();
		}
		public function toObject()
		{
			return $this->getAllJsonDataToObject();
		}
    }
